package roboSoccer;

/**
 * Behavior during which the player will go towards the ball.
 * 
 * @author Ben Hopkins
 * 
 */
public class PbChaseBall extends PlayerBehavior {

	private boolean atBall;
	
	/**
	 * Constructor does standard operation of giving superclass the host player
	 * and the name.
	 * 
	 * @param host
	 *            The player who is exhibiting this behavior.
	 */
	public PbChaseBall(RoboPlayer host) {
		super(host, "ChaseBall");
		atBall = false;
	}

	public boolean leaveState(GameState game) {
		return false;
	}

	/**
	 * This sends the robot towards the ball with commands to goToLocation.
	 */
	public void update(GameState game) {
		Vector robotFront = Vector.add(host.position,
				new Vector(host.position.direction, RoboPlayer.sizeRadius*1.5f, true));
		atBall = game.ball.position.distance(robotFront) < FieldObject.sizeRadius*1.5;
		host.controller.dribble();
		if(!atBall)
		{
				message = "Go to ball";
				host.controller.goToLocation(game, host, game.ball.position, 3, 15, 0,
						RoboPlayer.sizeRadius + FieldObject.sizeRadius);
				host.controller.turnTowardsLocation(game, host, game.ball.position, 5, 5);
		}
		else
		{
			host.team.onBall++;
			message = "Line up for shot";
			if(host.controller.goToLocation(game, host, host.team.farQuarter.center(), 3, 10, 50, 5)
					|| host.controller.turnTowardsLocation(game, host, host.team.targetGoal.center(), 5, 2))
				host.controller.kick(50);
		}
	}

}